package com.mobile.collect.activitys;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mobile.collect.R;
import com.mobile.collect.beans.DevicePicInfo;
import com.mobile.collect.beans.DevicePointInfo;
import com.mobile.collect.beans.SealPointInfo;
import com.mobile.collect.beans.TypeInfo;
import com.mobile.collect.db.EntityManager;
import com.mobile.collect.dialogs.BottomListDialog;
import com.mobile.collect.dialogs.v3.TipDialog;
import com.mobile.collect.interfaces.PopWindowInterface;
import com.mobile.collect.present.PicPointPresent;
import com.mobile.collect.utils.FileUtil;
import com.mobile.collect.utils.XMLUtil;
import com.mobile.collect.view.DescDialog;
import com.mobile.collect.view.InputDialog;
import com.mobile.collect.view.PopWindowEditText;
import com.mobile.collect.view.PopWindowTextView;
import com.mobile.collect.view.TypeDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.droidlover.xdroidmvp.base.ListViewChildClickListener;
import cn.droidlover.xdroidmvp.kit.Kits;
import cn.droidlover.xdroidmvp.mvp.XActivity;

@Deprecated
public class AddDevicePicPointActivity extends XActivity<PicPointPresent> implements ListViewChildClickListener {

    boolean isUpdate;
    Long picDbId;
    Long pointDbId;
    //    DevicePointInfo targetInfo;
//    @BindView(R.id.heightEt)
//    TextView heightEt;
    @BindView(R.id.hardTv)
    PopWindowTextView hardTv;
    @BindView(R.id.hardeasonTv)
    PopWindowEditText hardeasonTv;
    @BindView(R.id.dangerTv)
    PopWindowTextView dangerTv;
    @BindView(R.id.dangerReasonTv)
    PopWindowEditText dangerReasonTv;
    //    @BindView(R.id.keepWarmTv)
//    PopWindowTextView keepWarmTv;
    @BindView(R.id.statusTv)
    PopWindowTextView statusTv;
    @BindView(R.id.box_hard_remark)
    LinearLayout box_hard_remark;
    @BindView(R.id.box_danger_remark)
    LinearLayout box_danger_remark;
    DevicePicInfo picInfo;
    @BindView(R.id.btn_add)
    ImageView btnAdd;

    //    DevicePointInfo devicePointInfo;
    List<TypeInfo> typeInfoList;
    ArrayList<SealPointInfo> dataList = new ArrayList<>();
    @BindView(R.id.contentLayout)
    LinearLayout contentLayout;


    @Override
    public int getContentViewId() {
        return R.layout.activity_adddevicepicpoint;
    }

    @Override
    public void initView() {
        super.initView();
        if (isUpdate) {
            titleTv.setText("修改点");
        } else {
            titleTv.setText("新增点");
        }
        //高度大于2米自动难于检测
//        heightEt.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//                try {
//                    String inputStr = s.toString();
//                    if (isNull(inputStr)) {
//                        hardTv.setText("否");
//                    } else {
//                        Double inputNumber = Double.parseDouble(inputStr);
//                        if (inputNumber >= 4.5) {
//                            hardTv.setText("是");
//                        } else {
//                            hardTv.setText("否");
//                        }
//                    }
//                } catch (Exception e) {
//                }
//            }
//        });


        //险于检测
//        dangerTv.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//                try {
//                    String inputStr = s.toString();
//                    if (isNull(inputStr)) {
//                        box_danger_remark.setVisibility(View.GONE);
//                    } else {
//                        if (inputStr.equals("是")) {
//                            box_danger_remark.setVisibility(View.VISIBLE);
//                        } else {
//                            box_danger_remark.setVisibility(View.GONE);
//                            dangerReasonTv.setText("");
//
//                        }
//                    }
//                } catch (Exception e) {
//                }
//            }
//        });

        hardTv.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                try {
                    String inputStr = s.toString();
                    if (isNull(inputStr)) {
                        box_hard_remark.setVisibility(View.GONE);
                    } else {
                        if (inputStr.equals("是")) {
                            box_hard_remark.setVisibility(View.VISIBLE);
                        } else {
                            box_hard_remark.setVisibility(View.GONE);
                            hardeasonTv.setText("");
                        }
                    }
                } catch (Exception e) {
                }
            }
        });

//        descTv.setMenuList(getP().getAllDescription());
    }

    DevicePointInfo targetInfo;

    private void setDefault() {
        if (picDbId == -1) {
            return;
        }
        targetInfo = EntityManager.getInstance().queryDevicePointInfoIById(pointDbId);
        if (null != targetInfo) {
            setPointInfo(targetInfo);
        }
    }


    private void setPointInfo(DevicePointInfo targetInfo) {
//        heightEt.setText(targetInfo.getHeight());
        String isYes = targetInfo.getCheckHardType();

        hardTv.setText(targetInfo.getCheckHardType());
        if (TextUtils.equals("是", isYes)) {
            hardeasonTv.setVisibility(View.VISIBLE);
        }
        hardeasonTv.setText(targetInfo.getCheckHardReason());
        isYes = targetInfo.getDangerCheck();
        dangerTv.setText(targetInfo.getDangerCheck());
        if (TextUtils.equals("是", isYes)) {
            dangerReasonTv.setVisibility(View.VISIBLE);
        }

        dangerReasonTv.setText(targetInfo.getDangerCheckReaseon());
//        keepWarmTv.setText(targetInfo.getHeatPreservation());
//        descTv.setText(targetInfo.getAdditionalDes());
        statusTv.setText(targetInfo.getStatus());
        if (!isUpdate) {
            targetInfo.set_ID(null);
        }
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        picInfo = EntityManager.getInstance().queryDevicePicIById(picDbId);
        setDefault();
    }

    @Override
    public void onRightClick(View view) {
        super.onRightClick(view);
        saveSealPoints();
//        DevicePointInfo devicePointInfo = getPointInfo();
//        if (null != devicePointInfo) {
//            Long id = EntityManager.getInstance().savePointInfo(devicePointInfo);
//            devicePointInfo.set_ID(id);
//            startActivity(new Intent(this, DevicePointsActivity.class));
//            finish();
//        }

    }

    @Override
    public void getDataFromServer() {
        SealPointInfo sealPointInfo = null;

        if (isUpdate) {
            DevicePointInfo targetInfo = EntityManager.getInstance().queryDevicePointInfoIById(pointDbId);
            sealPointInfo = new SealPointInfo();
            sealPointInfo.setPoint_ID(pointDbId);
            sealPointInfo.setUpdate(true);
            sealPointInfo.setCount(1);
            sealPointInfo.setChildTypeEn(targetInfo.getChildTypeEn());
            sealPointInfo.setChildType(targetInfo.getChildType());
            sealPointInfo.setDimen(targetInfo.getDiameter());
            sealPointInfo.setType(targetInfo.getType());
            sealPointInfo.setTypeEn(targetInfo.getTypeEn());
            sealPointInfo.setDesc(targetInfo.getAdditionalDes());
            sealPointInfo.setKeepWarm(targetInfo.getHeatPreservation());
            sealPointInfo.setHigh(targetInfo.getHeight());
            addNewView(0, sealPointInfo);
        } else {
            if (Kits.Empty.check(dataList)) {
                sealPointInfo = new SealPointInfo();
                sealPointInfo.setCount(1);
                addNewView(0, sealPointInfo);
            } else {
                for (int i = 0; i < dataList.size(); i++) {
                    addNewView(i, dataList.get(i));
                }
            }

        }
        XMLUtil xmlUtil = new XMLUtil(FileUtil.getFromAssets(this, "childtype.xml"));
        typeInfoList = xmlUtil.parserChildType();
    }


    @Override
    public void getIntentData() {
        picDbId = getIntent().getLongExtra("picdbid", -1);
        pointDbId = getIntent().getLongExtra("pointdbid", -1);
        isUpdate = getIntent().getBooleanExtra("isupdate", false);
    }

    @Override
    public PicPointPresent newP() {
        return new PicPointPresent();
    }


    public static void lunch(Activity context, boolean isUpdate, Long picDbId, Long pointDbId) {
        Intent intent = new Intent(context, AddUnitPointActivity.class);
        intent.putExtra("picdbid", picDbId);
        intent.putExtra("isupdate", isUpdate);
        intent.putExtra("pointdbid", pointDbId);
        context.startActivityForResult(intent, 0x001);
    }


    protected void showBottomDialog(final TextView view, List<String> dicList) {
        BottomListDialog bottomListDialog = new BottomListDialog(this, new BottomListDialog.BottomListSelectListener() {
            @Override
            public void itemClickCallback(String selectStr) {
                view.setText(selectStr);
                setReleven(view.getId(), selectStr);
            }
        });
        bottomListDialog.disPlay(dicList);
    }

    private void setReleven(int tvId, String txt) {
        switch (tvId) {
            case R.id.hardTv:
                if (txt.equals("是")) {
                    box_hard_remark.setVisibility(View.VISIBLE);
                    dangerReasonTv.setText(getP().getHardReasonList().get(0));

                } else {
                    box_hard_remark.setVisibility(View.GONE);
                    hardeasonTv.setText("");
                }
                break;
            case R.id.dangerTv:
                if (txt.equals("是")) {
                    dangerReasonTv.setText(getP().getDangerReasonList().get(0));
                    box_danger_remark.setVisibility(View.VISIBLE);
                } else {
                    box_danger_remark.setVisibility(View.GONE);


                }
                break;
        }
    }


    @OnClick({R.id.rightIv, R.id.expandEt, R.id.addPointBtn})
    public void onViewClicked(View view) {
        List<String> list = new ArrayList<>();
        TextView valueTv = null;
        switch (view.getId()) {
            case R.id.rightIv:
                Long deviceDbId = picInfo.getParentId();
                CopyPicListActivity.lunch(this, deviceDbId, picDbId, true);
                return;
            case R.id.expandEt:
//                showFloorPop("扩展号", expandEt);
                break;
            case R.id.zhijingEt:
//                showNumberDialog("直径", zhijingEt, 0, false);
                break;
//            case R.id.heightEt:
//                showNumberDialog("高度", heightEt, 0);
//                break;
            case R.id.addPointBtn:
                SealPointInfo newInfo = new SealPointInfo();
                addNewView(dataList.size(), newInfo);
                break;
        }
    }

//    private void startSealPoint() {
//        DevicePointInfo devicePointInfo = getPointInfo();
//        if (null != devicePointInfo) {
//            Intent intent = new Intent(this, SealingPointActivity.class);
//            intent.putExtra("devicePointInfo", devicePointInfo);
//            if (null != hadAddpointInfos) {
//                intent.putExtra("points", hadAddpointInfos);
//            }
//            startActivityForResult(intent, 0x002);
//        }
//    }


    @Override
    public int getTitleLayoutResId() {
        return 0;
    }


    private void checkInput() {
        String status = statusTv.getText().toString();
        if (TextUtils.isEmpty(status)) {
            TipDialog.show(this, "请选择物料状态", TipDialog.TYPE.WARNING);
            return;
        }
//        String height = heightEt.getText().toString();
//        if (TextUtils.isEmpty(height)) {
//            TipDialog.show(this, "请输入高度", TipDialog.TYPE.WARNING);
//            return;
//        }


        String hard = hardTv.getText().toString();
        String hardreason = hardeasonTv.getText().toString();

        if (hard.equals("是") && TextUtils.isEmpty(hardreason)) {
            TipDialog.show(this, "请输入难于检测原因", TipDialog.TYPE.WARNING);
            return;
        }
        String dangerCheck = dangerTv.getText().toString();
        String dangerReason = dangerReasonTv.getText().toString();

        if (dangerCheck.equals("是") && TextUtils.isEmpty(dangerReason)) {
            TipDialog.show(this, "请输入险于检测原因", TipDialog.TYPE.WARNING);
            return;
        }
    }


    public DevicePointInfo getPointInfo() {
        String status = statusTv.getText().toString();
        if (TextUtils.isEmpty(status)) {
            TipDialog.show(this, "请选择物料状态", TipDialog.TYPE.WARNING);
            return null;
        }
//        String height = heightEt.getText().toString();
//        if (TextUtils.isEmpty(height)) {
//            TipDialog.show(this, "请输入高度", TipDialog.TYPE.WARNING);
//            return null;
//        }


        String hard = hardTv.getText().toString();
        String hardreason = hardeasonTv.getText().toString();

        if (hard.equals("是") && TextUtils.isEmpty(hardreason)) {
            TipDialog.show(this, "请输入难于检测原因", TipDialog.TYPE.WARNING);
            return null;
        }
        String dangerCheck = dangerTv.getText().toString();
        String dangerReason = dangerReasonTv.getText().toString();

        if (dangerCheck.equals("是") && TextUtils.isEmpty(dangerReason)) {
            TipDialog.show(this, "请输入险于检测原因", TipDialog.TYPE.WARNING);
            return null;
        }


        DevicePointInfo newPointInfo = new DevicePointInfo();
        newPointInfo.setPointDircetion("up");
        newPointInfo.setLineType2("1x");
        newPointInfo.setDirection(90);

        newPointInfo.setDeviceName(picInfo.getDeviceName());
        newPointInfo.setStatus(status);
        newPointInfo.setCheckHardType(hard);
        if (hard.equals("是")) {
            newPointInfo.setCheckHardReason(hardreason);
        } else {
            newPointInfo.setCheckHardReason("");
        }
        newPointInfo.setDangerCheck(dangerCheck);
        newPointInfo.setDangerCheckReaseon(dangerReason);
//        newPointInfo.setHeight(height);
//        newPointInfo.setHeatPreservation(keepWarmTv.getText().toString());
        newPointInfo.setParentId(picDbId);
        return newPointInfo;
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }


    public void OnMoreClick(View view) {
        ViewGroup box = (ViewGroup) view.getParent();
        PopWindowInterface editText = null;
        for (int i = 0; i < box.getChildCount(); i++) {
            if (box.getChildAt(i) instanceof PopWindowInterface) {
                editText = (PopWindowInterface) box.getChildAt(i);
            }
        }
        if (editText != null) {
            if (editText instanceof EditText) {
                showIME(false, (EditText) editText);
            }
            editText.showMenu();
        }
    }

    //软键盘打开与收起
    public void showIME(boolean show, EditText editText) {
        if (editText == null) {
            return;
        }
        if (show) {
            editText.requestFocus();
            editText.setFocusableInTouchMode(true);
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
        } else {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
        }
    }

    @Override
    public boolean isNotWorkConnect() {
        return false;
    }

    private void showHighDialog(String label, final SealPointInfo sealPointInfo, final TextView clickView, int maxCount) {
        InputDialog numberPop = new InputDialog(this, label, new InputDialog.InputCompleteListener() {
            @Override
            public void onInputComplete(String value) {
                clickView.setText(value);
                getP().setDefaultHigh(value);
                SealPointInfo aboutPoint = sealPointInfo.getAboutFaLan();
                if (null != aboutPoint) {
                    aboutPoint.getHighTv().setText(value);
                }
            }
        }, maxCount);
        numberPop.disPlay(InputDialog.broad2);

    }

    private void addNewView(int position, SealPointInfo sealPointInfo) {
        if (!dataList.contains(sealPointInfo)) {
            dataList.add(position, sealPointInfo);
        }
        View view = getP().getSealPointView(sealPointInfo, position, this);
        contentLayout.addView(view, position);
    }

    private void showDimenDialog(final SealPointInfo info, final String label) {
        InputDialog numberPop = new InputDialog(this, label, new InputDialog.InputCompleteListener() {
            @Override
            public void onInputComplete(String value) {
                info.setDimen(value);
                if (info.getAboutFaLan() != null) {
                    info.getAboutFaLan().getDimenTv().setText(value);
                    info.getAboutFaLan().setDimen(value);
                }
                info.getDimenTv().setText(value);
            }
        }, 0);
        numberPop.disPlay(InputDialog.broad6, true);
    }

//    private void showDelPointTip(final SealPointInfo pointInfo, final int position) {
//        SubmitTipDialog submitTipDialog = new SubmitTipDialog(this, new SubmitTipDialog.SlectClickListener() {
//            @Override
//            public void submitClick() {
//                EntityManager.getInstance().delDevicePointInfo(devicePointInfo);
//                dataList.remove(pointInfo);
//                contentLayout.removeViewAt(position);
//            }
//
//            @Override
//            public void cancelClick() {
//
//            }
//        });
//        submitTipDialog.display("温馨提示", "是否删除该密封点?", "取消", "删除");
//    }


    private void showDescDialog(String label, final SealPointInfo sealPointInfo, final TextView clickView) {
        DescDialog numberPop = new DescDialog(this, label, new InputDialog.InputCompleteListener() {
            @Override
            public void onInputComplete(String value) {
                clickView.append(value);
                SealPointInfo aboutPoint = sealPointInfo.getAboutFaLan();
                if (null != aboutPoint) {
                    aboutPoint.getDescTv().setText(clickView.getText().toString());
                }

            }
        });
        numberPop.show();
    }

    @Override
    public void itemChildClick(int position, int viewId) {
        switch (viewId) {
            case R.id.childTypeTv:
                showTypeDialog(dataList.get(position), "组件子类型", position);
                break;
            case R.id.dimenTv:
                showDimenDialog(dataList.get(position), "尺寸");
                break;
            case R.id.numberTv:
                SealPointInfo info = dataList.get(position);
                String oldTxt = info.getCount() == 0 ? "" : String.valueOf(info.getCount());
                showCountDialog(info, "数量", oldTxt, 0, true);
                break;
            case R.id.descMoreIv:
                showDescDialog("附加描述", dataList.get(position), dataList.get(position).getDescTv());
                break;
            case R.id.highTv:
                showHighDialog("高度", dataList.get(position), dataList.get(position).getHighTv(), 0);
                break;
            case R.id.keepWarmTv:
                showKeepWarmDialog(dataList.get(position), dataList.get(position).getKeepWarmTv());
                break;
            case R.id.delIv:
                if (dataList.size() == 1) {
                    TipDialog.show(this, "至少添加一个密封点", TipDialog.TYPE.WARNING);
                    break;
                }
                SealPointInfo pointInfo = dataList.get(position);
                if (pointInfo.getPoint_ID() != null) {
                    EntityManager.getInstance().delDevicePointInfoById(pointInfo.getPoint_ID());
                }
                dataList.remove(pointInfo);
                contentLayout.removeViewAt(position);
                break;
        }
    }

    private void showTypeDialog(final SealPointInfo info, final String label, final int position) {
        TypeDialog numberPop = new TypeDialog(this, label, new TypeDialog.TypeCompleteListener() {
            @Override
            public void onInputComplete(String value) {

            }

            @Override
            public void setType(TypeInfo childTypeInfo) {
                info.setType(childTypeInfo.getTypeName());
                info.setTypeEn(childTypeInfo.getTypeCode());
                info.setChildTypeEn(childTypeInfo.getChildCode());
                info.setChildType(childTypeInfo.getChildName());
                info.setCount(1);
                toAutoFalan(info, position);
                info.getChildTypeTv().setText(childTypeInfo.getChildCode());
            }
        });
        numberPop.show();
    }

    private void toAutoFalan(SealPointInfo info, int position) {
        SealPointInfo falan = null;
        switch (info.getChildTypeEn()) {
            case "GV":
            case "CV":
            case "BUV":
            case "GLV":
            case "BV": {
                falan = info.getAboutFaLan();
                if (null == falan) {
                    falan = new SealPointInfo();
                    falan.setCount(2);
                    falan.setType("法兰");
                    falan.setTypeEn("F");
                    falan.setChildTypeEn("F");
                    falan.setChildType("法兰");
                    addNewView(position + 1, falan);
                }
                info.setAboutFaLan(falan);
            }

        }
    }

    private void showCountDialog(final SealPointInfo info, final String label, final String oldTxt, int maxCount, boolean onlyNum) {
        InputDialog numberPop = new InputDialog(this, label, new InputDialog.InputCompleteListener() {
            @Override
            public void onInputComplete(String value) {
                try {
                    info.setCount(Integer.parseInt(value));
                    info.getNumberTv().setText(value);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }

            }
        }, maxCount);
        numberPop.disPlay(InputDialog.broad1, true);
    }


    private void saveSealPoints() {

        String status = statusTv.getText().toString();
        if (TextUtils.isEmpty(status)) {
            TipDialog.show(this, "请选择物料状态", TipDialog.TYPE.WARNING);
            return;
        }
//        String height = heightEt.getText().toString();
//        if (TextUtils.isEmpty(height)) {
//            TipDialog.show(this, "请输入高度", TipDialog.TYPE.WARNING);
//            return;
//        }


        String hard = hardTv.getText().toString();
        String hardreason = hardeasonTv.getText().toString();

        if (hard.equals("是") && TextUtils.isEmpty(hardreason)) {
            TipDialog.show(this, "请输入难于检测原因", TipDialog.TYPE.WARNING);
            return;
        }
        String dangerCheck = dangerTv.getText().toString();
        String dangerReason = dangerReasonTv.getText().toString();

        if (dangerCheck.equals("是") && TextUtils.isEmpty(dangerReason)) {
            TipDialog.show(this, "请输入险于检测原因", TipDialog.TYPE.WARNING);
            return;
        }

        DevicePointInfo newPointInfo;
        if (isUpdate) {
            newPointInfo = targetInfo;
        } else {
            newPointInfo = new DevicePointInfo();
            newPointInfo.setPointDircetion("up");
            newPointInfo.setLineType2("1x");
            newPointInfo.setDirection(90);
        }
        newPointInfo.setDeviceName(picInfo.getDeviceName());
        newPointInfo.setStatus(status);
        newPointInfo.setCheckHardType(hard);
        newPointInfo.setCheckHardReason(hardreason);
        newPointInfo.setDangerCheck(dangerCheck);
        newPointInfo.setDangerCheckReaseon(dangerReason);
//        newPointInfo.setHeight(height);
//        newPointInfo.setHeatPreservation(keepWarmTv.getText().toString());
        newPointInfo.setParentId(picDbId);

        for (SealPointInfo sealPointInfo : dataList) {
            String childType = sealPointInfo.getChildTypeTv().getText().toString();
            String dimen = sealPointInfo.getDimenTv().getText().toString();
            String count = sealPointInfo.getNumberTv().getText().toString();
            String high = sealPointInfo.getHighTv().getText().toString();
            String keepwarm = sealPointInfo.getKeepWarmTv().getText().toString();
            String desc = sealPointInfo.getDescTv().getText().toString();

            if (TextUtils.isEmpty(childType)) {
                TipDialog.show(this, "密封点类型不能为空", TipDialog.TYPE.WARNING);
                return;
            }
            if (TextUtils.isEmpty(dimen)) {
                TipDialog.show(this, "密封点尺寸不能为空", TipDialog.TYPE.WARNING);
                return;
            }
            if (TextUtils.isEmpty(count) || TextUtils.equals(count, "0")) {
                TipDialog.show(this, "密封点尺寸不能为空", TipDialog.TYPE.WARNING);
                return;
            }
            if (TextUtils.equals(count, "0")) {
                TipDialog.show(this, "密封点尺寸不能为0", TipDialog.TYPE.WARNING);
                return;
            }
            if (TextUtils.isEmpty(high)) {
                TipDialog.show(this, "密封点高度不能为空", TipDialog.TYPE.WARNING);
                return;
            }
            if (TextUtils.isEmpty(keepwarm)) {
                TipDialog.show(this, "密封点保温不能为空", TipDialog.TYPE.WARNING);
                return;
            }
            if (TextUtils.isEmpty(desc)) {
                TipDialog.show(this, "密封点附加描述不能为空", TipDialog.TYPE.WARNING);
                return;
            }
        }


        List<DevicePointInfo> newPointList = new ArrayList<>();
        int expend = 0;
        int count = dataList.size();
        for (int i = 0; i < count; i++) {
            SealPointInfo sealPointInfo = dataList.get(i);
            String childTypeEn = sealPointInfo.getChildTypeEn();
            if (childTypeEn.equals("KP") || childTypeEn.equals("空冷丝堵")) {
                DevicePointInfo pointInfo = getNewDevicePointInfo(newPointInfo, sealPointInfo, expend);
                pointInfo.setDeviceCount(sealPointInfo.getCount());
                newPointList.add(pointInfo);
                expend++;
            } else {
                for (int r = 0; r < sealPointInfo.getCount(); r++) {
                    DevicePointInfo pointInfo = getNewDevicePointInfo(newPointInfo, sealPointInfo, expend);
                    pointInfo.setDeviceCount(1);
                    expend++;
                    newPointList.add(pointInfo);
                }
            }

        }
        EntityManager.getInstance().savePointInfo(newPointList);
        sendAddPointEvent();
        startActivity(new Intent(this, DevicePointsActivity.class));
    }

    private DevicePointInfo getNewDevicePointInfo(DevicePointInfo newPointInfo, SealPointInfo sealPointInfo, int expend) {
        DevicePointInfo pointInfo;
        pointInfo = newPointInfo.getNewPointInfo();
        if (sealPointInfo.getPoint_ID() != null) {
            long point_id = sealPointInfo.getPoint_ID();
            pointInfo.set_ID(point_id);
            sealPointInfo.setPoint_ID(null);
        } else {
            pointInfo.set_ID(null);
        }
        pointInfo.setType(sealPointInfo.getType());
        pointInfo.setTypeEn(sealPointInfo.getTypeEn());
        pointInfo.setChildType(sealPointInfo.getChildType());
        pointInfo.setChildTypeEn(sealPointInfo.getChildTypeEn());
        pointInfo.setDiameter(sealPointInfo.getDimenTv().getText().toString());
        pointInfo.setAdditionalDes(sealPointInfo.getDescTv().getText().toString());
        pointInfo.setHeight(sealPointInfo.getHighTv().getText().toString());
        pointInfo.setHeatPreservation(sealPointInfo.getKeepWarmTv().getText().toString());
        newPointInfo.setLineType2("1x");
        pointInfo.setBgType(0);

        if (pointInfo.get_ID() == null) {
            int indexX = expend % 10;
            int indexY = expend / 10;
            pointInfo.setScaleY(0.1f + 0.1f * indexX);
            pointInfo.setScaleX(0.1f + 0.1f * indexY);
        }
        return pointInfo;
    }

    private void showKeepWarmDialog(final SealPointInfo sealPointInfo, final TextView clickView) {
        BottomListDialog listDialog = new BottomListDialog(this, new BottomListDialog.BottomListSelectListener() {
            @Override
            public void itemClickCallback(String selectStr) {
                clickView.setText(selectStr);
                getP().setDefaultKeepWarm(selectStr);
                SealPointInfo aboutPoint = sealPointInfo.getAboutFaLan();
                if (null != aboutPoint) {
                    aboutPoint.getKeepWarmTv().setText(selectStr);
                }
            }
        });
        List<String> list = new ArrayList<>();
        list.add("是");
        list.add("否");
        listDialog.disPlay(list);
    }

    public String getLastDesc() {
        String lasetDesc = "";
        for (int i = dataList.size() - 1; i >= 0; i--) {
            SealPointInfo pointInfo = dataList.get(i);
            TextView descTv = pointInfo.getDescTv();
            if (null != descTv) {
                String desc = pointInfo.getDescTv().getText().toString();
                if (!TextUtils.isEmpty(desc)) {
                    lasetDesc = desc;
                    break;
                }
            }
        }
        return lasetDesc;
    }

    private void sendAddPointEvent(){
//        BusProvider.getBus().post(new AddPointEvent());
    }
}
